import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[],
    list2:[]
  },
  mutations: {
    get_list(state,item){
      state.list=[...item]
      state.list2=[...item]
    },
    // 删除
    del_list(state,index){
      state.list.splice(index,1)
    },
    // 添加
    add_list(state,item){
      state.list.unshift(item)
    },
    search(state,item){
      let temp=[]
      state.list.forEach(Element=>{
        if(Element.name.includes(item)){
          temp.push(Element)
        }
      })
      state.list=temp

      if(item==''){
        state.list=state.list2
      }
    }
  },
  actions: {
    GET_list(context){
      axios.get('/shopList(1).json').then(res=>{
        context.commit('get_list',res.data)
      })
    },
    // 删除
    DEL_list(context,index){
      context.commit('del_list',index)
    },
    ADD_list(context,item){
      context.commit('add_list',item)
    },
    search(context,item){
      context.commit('search',item)
    }
  },
  modules: {
  }
})
